#!/usr/bin/python

import serial

SerialPort = '/dev/ttyUSB_PL2303'
BaudRate = 9600
STATUSREQSTRING = "cmd: request status\n"
TRIGGERSTRING = "cmd: trigger door\n"
RESPONSEOPEN = "rsp: door open\n"
RESPONSECLOSED = "rsp: door closed\n"

class SerialCom():
   
   def __init__(self):
      self.Connected = False
      try:
         self.ser = serial.Serial(SerialPort,BaudRate,timeout=1)
         print self.ser.portstr
         self.Connected = True
      except serial.SerialException, e:
         print "No Serial Com"
   def SendTriggerCmd(self):
      if(self.Connected == True):
         self.ser.write(TRIGGERSTRING)
      return
      
   def SendRequestCmd(self):
      
      if(self.Connected == True):
         self.ser.write(STATUSREQSTRING)
         line = self.ser.readline()
         print line
         if line == RESPONSECLOSED:
            ret = 1
         elif line == RESPONSEOPEN:
            ret = 0
         else:
            ret = -1
      else:
         ret = -1
         
      return ret
   
   def __del__(self):
      if self.Connected: 
         if self.ser.isOpen():
            self.ser.close()
